package com.daguchuangyi.erp_android.search;

import android.graphics.Color;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ForegroundColorSpan;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.daguchuangyi.erp_android.ErpApplication;
import com.daguchuangyi.erp_android.R;
import com.daguchuangyi.erp_android.model.product.Product;
import com.daguchuangyi.erp_android.production_detail.ProductDetailActivity;
import com.jude.utils.JUtils;

import java.util.List;

/**
 * Created by lijianjian on 2018/3/15.
 */

public class SearchResultAdapter extends BaseQuickAdapter<Product, BaseViewHolder> {

    private String mKeyWords;

    public SearchResultAdapter(@Nullable List<Product> data) {
        super(R.layout.item_product_search_result_layout, data);
    }

    @Override
    protected void convert(BaseViewHolder helper, Product item) {
        TextView titleTv = helper.getView(R.id.title_tv);
        String productTitleStr = item.getPNO();
        SpannableString spannableString = new SpannableString(productTitleStr);
//
        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#4c071c4e")), 0, productTitleStr.length(),
                Spanned.SPAN_EXCLUSIVE_INCLUSIVE);
        JUtils.Log("keyword is : " + mKeyWords);
        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#64a2f0")), 0, mKeyWords.length(),
                Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        titleTv.setText(spannableString);

        helper.itemView.setOnClickListener(view -> {
            ProductDetailActivity.start(ErpApplication.getAppContext(), item.getId());
        });

    }

    public void freshData(List<Product> products, String keyWord) {
        mKeyWords = keyWord;
        setNewData(products);
    }
}
